﻿using System;
using System.Linq;
using System.ServiceModel.Syndication;
using Api.Model;
using NUnit.Framework;

namespace RssProxyService.Test.Unit
{
    [TestFixture]
    public class RssMapperTests
    {
        private RssMapper rssMapper;

        [SetUp]
        public void SetUpTest()
        {
            this.rssMapper = new RssMapper();
        }

        [Test]
        public void GetMappedItems_InvalidInput_IEnumerableEmptyReturned()
        {
            string rssFeed = "agegegegsad";

            var articles = rssMapper.GetMappedItems(rssFeed);
            Assert.AreEqual(Enumerable.Empty<Article>(), articles);
        }

        [Test]
        public void GetMappedItems_OneItemInRss_ArticleCountOne()
        {
            SyndicationItem[] items = new SyndicationItem[] { new SyndicationItem() };
            SyndicationFeed feed = new SyndicationFeed(items);

            var articles = rssMapper.GetMappedItems(feed);
            Assert.AreEqual(1, articles.Count());
        }

        [Test]
        public void GetMappedItems_TwoItemInRss_ArticleCountTwo()
        {
            SyndicationItem[] items = new SyndicationItem[] { new SyndicationItem(), new SyndicationItem() };
            SyndicationFeed feed = new SyndicationFeed(items);

            var articles = rssMapper.GetMappedItems(feed);

            Assert.AreEqual(2, articles.Count());
        }

        [Test]
        public void MapSingleArticle_TitlePresent_TitleMappedProperly()
        {
            SyndicationItem syndicationItem = new SyndicationItem();
            syndicationItem.Title = new TextSyndicationContent("Nowa myszka w sprzedaży!");

            var article = rssMapper.MapSingleArticle(syndicationItem);

            Assert.AreEqual("Nowa myszka w sprzedaży!", article.Title);
        }

        [Test]
        public void MapSingleArticle_PublicationDatePresent_PublicationDateMappedProperly()
        {
            SyndicationItem syndicationItem = new SyndicationItem();
            syndicationItem.PublishDate = new DateTimeOffset(new DateTime(2007, 5, 19, 16, 41, 43));

            var article = rssMapper.MapSingleArticle(syndicationItem);

            var expectedDate = new DateTime(2007, 5, 19, 16, 41, 43);
            Assert.AreEqual(expectedDate, article.PublishDate.Value);
        }

        [Test]
        public void MapSingleArticle_LinkPresent_LinkMappedToOneOfItems()
        {
            SyndicationItem syndicationItem = new SyndicationItem();
            syndicationItem.Links.Add(new SyndicationLink(new Uri("http://przykladowy_adres.pl/rss/info001.html",UriKind.Absolute)));

            var article = rssMapper.MapSingleArticle(syndicationItem);

            var link = article.Items.Where(item => item.Type == "link").FirstOrDefault();
            Assert.AreEqual("http://przykladowy_adres.pl/rss/info001.html", link.Value);
        }

        [Test]
        public void MapSingleArticle_DescriptionPresent_DescriptionPresentMappedToOneOfItems()
        {
            SyndicationItem syndicationItem = new SyndicationItem();
            syndicationItem.Summary = new TextSyndicationContent("Nowa myszka optyczna firmy Mouse Ltd. wchodzi na rynek.");

            var article = rssMapper.MapSingleArticle(syndicationItem);

            var description = article.Items.Where(item => item.Type == "text").FirstOrDefault();
            Assert.AreEqual("Nowa myszka optyczna firmy Mouse Ltd. wchodzi na rynek.", description.Value);
        }

        [Test]
        public void MapSingleArticle_TitleNotPresent_TitleNull()
        {
            SyndicationItem syndicationItem = new SyndicationItem();

            var article = rssMapper.MapSingleArticle(syndicationItem);

            Assert.IsNullOrEmpty(article.Title);
        }

        [Test]
        public void MapSingleArticle_PublicationDateNotPresent_PublicationDateNull()
        {
            SyndicationItem syndicationItem = new SyndicationItem();

            var article = rssMapper.MapSingleArticle(syndicationItem);

            Assert.AreEqual(null, article.PublishDate);
        }

        [Test]
        public void MapSingleArticle_LinkNotPresent_NoLinkInItems()
        {
            SyndicationItem syndicationItem = new SyndicationItem();

            var article = rssMapper.MapSingleArticle(syndicationItem);
            var link = article.Items.Where(item => item.Type == "link").FirstOrDefault();

            Assert.AreEqual(null, link);
        }

        [Test]
        public void MapSingleArticle_DescriptionNotPresent_NoDescriptionInMapping()
        {
            SyndicationItem syndicationItem = new SyndicationItem();

            var article = rssMapper.MapSingleArticle(syndicationItem);

            var description = article.Items.Where(item => item.Type == "text").FirstOrDefault();
            Assert.AreEqual(null, description);
        }
    }
}
